package com.ruix.app.ui.main;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.IMulItemViewType;
import com.ruix.app.adapter.superadapter.OnItemClickListener;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.ClassesMapper;
import com.ruix.app.model.Classes;
import com.ruix.app.model.User;
import com.ruix.app.network.CacheObserver;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.ClassListAdapter;
import com.ruix.app.ui.base.BaseFragment;
import com.ruix.app.ui.stratum.ClassesStratumActivity;
import com.ruix.app.ui.stratum.OrganizCircleListActivity;
import com.ruix.app.util.LogUitl;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;


/**
 * Created by hadexs on 2017/4/10.
 */

public class GradeFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener,SwipeRefreshLayout.OnRefreshListener {

//    private SpringView springView;
    private ListView classesList;
    private ClassListAdapter adapter;
    //    private ArrayList dataList;
    private ArrayList clasessList;
//    private ArrayList sugestList;
    private SwipeRefreshLayout mRefreshLayout;

    @Override
    protected int loadViewLayout() {
        return R.layout.fragment_group;
    }

    @Override
    protected void initView() {
        mRefreshLayout = (SwipeRefreshLayout) mFragmentView.findViewById(R.id.refresh_layout);
        mRefreshLayout.setColorSchemeResources(R.color.bg_nav_default
                , R.color.bg_nav_yellow
                , R.color.bg_default
                , R.color.text_color_pink);
//        springView = (SpringView) mFragmentView.findViewById(R.id.spring_view);
//        springView.setType(SpringView.Type.FOLLOW);
//        springView.setGive(SpringView.Give.TOP);
//        springView.setHeader(new DefaultHeader(this.getContext()));
//        springView.setFooter(new DefaultFooter(this.getContext()));

        classesList = (ListView) mFragmentView.findViewById(R.id.lv_classes);
        adapter = new ClassListAdapter(this.getContext(), null, new IMulItemViewType<Classes>() {
            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public int getItemViewType(int position, Classes classes) {
                if (classes.isOrganizClass()){
                    return 1;
                }
                return 0;
            }

            @Override
            public int getLayoutId(int viewType) {
                int layout = 0;
                switch (viewType) {
                    case 0:
                        layout = R.layout.item_grade_list; //班级
                        break;
                    case 1:
                        layout = R.layout.item_grade_list_default; //组织圈子
                        break;
                }
                return layout;
            }
        });
        classesList.setAdapter(adapter);

    }

    @Override
    protected void initLocalData() {
        clasessList = new ArrayList();
        loadCircleListByCache();

//        loadSugestList();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadClassesList(false);
    }

    @Override
    protected void initListener() {
        classesList.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);

//        springView.setListener(new SpringView.OnFreshListener() {
//            @Override
//            public void onRefresh() {
//                loadClassesList(true);
//            }
//
//            @Override
//            public void onLoadmore() {
//
//            }
//        });
    }

    @Override
    public void onClick(View v) {

    }

    private void loadCircleListByCache(){
        CacheFactory.loadCircleList(UserManager.getCurrentUser().getID(),new CacheObserver<ArrayList<Classes>>(){
            @Override
            public void onNext(ArrayList<Classes> value) {
                if (value != null) {
                    GradeFragment.this.clasessList = value;
                    updateView();
                }
            }

        });
    }

    private void loadClassesList(final boolean refresh) {
        RequestFactory.classesList(new ClassesMapper(), new RXObserver<ArrayList<Classes>>() {

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<Classes> value) {
                if (value != null){
                    if (refresh && clasessList != null && value.size() == clasessList.size()){
                        showErrorTip("没有新内容了，请稍后刷新");
                    }
                    GradeFragment.this.clasessList = value;
                    updateView();
                    //缓存
                    CacheFactory.cacheCircleList(UserManager.getCurrentUser().getID(),value);
                }
            }


            @Override
            public void onError(Throwable e) {
                resolveException(e);
                if (refresh){
                    mRefreshLayout.setRefreshing(false);
                } else {
                    dismissLoading();
                }
            }

            @Override
            public void onComplete() {
                if (refresh){
                    mRefreshLayout.setRefreshing(false);
                } else {
                    dismissLoading();
                }
            }
        });
    }

    private void loadSugestList() {
        RequestFactory.classSugestList(new ClassesMapper(), new RXObserver<ArrayList<Classes>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList value) {
            }

            @Override
            public void onError(Throwable e) {
                resolveException(e);
            }

            @Override
            public void onComplete() {
            }
        });


    }

    private void updateView() {
        adapter.replaceAll(clasessList);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (this.clasessList != null) {
            Classes classes = (Classes) clasessList.get(position);
            if (classes.isOrganizClass()){
                Intent intent = OrganizCircleListActivity.newIntent(this.getActivity(), classes);
                getActivity().startActivity(intent);
            } else {
                UserManager.sharedInstance().setCurrentClass(classes);
                Intent intent = ClassesStratumActivity.newIntent(this.getActivity(), classes,null);
                getActivity().startActivity(intent);
                if (classes.isHaseNewMessage()) {
                    classes.setHaseNewMessage(false);
                    adapter.notifyDataSetHasChanged();
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        loadClassesList(true);
    }
}
